<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
    >
      <el-form-item
        label="客户名称"
        prop="corpName"
      >
        <el-input
          v-model="queryParams.corpName"
          placeholder="请输入客户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="任务名称"
        prop="name"
      >
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="任务类别"
        prop="type"
      >
        <el-select
          v-model="queryParams.type"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="任务状态"
        prop="status"
      >
        <el-select
          v-model="queryParams.status"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.job_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['task:basic:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['task:basic:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selected.length === 0"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['task:basic:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        v-if="!corpName"
        :show-search.sync="showSearch"
        @queryTable="getList"
      />
    </el-row>

    <el-table
      ref="table"
      v-loading="loading"
      :data="taskList"
      @selection-change="(selection) => selected = selection"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="任务名称"
        prop="name"
        width="200"
      />
      <el-table-column
        v-if="!corpName"
        label="客户名称"
        prop="corpName"
        width="200"
      />
      <el-table-column label="任务类型">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_business_type"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="当前状态"
        width="100"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_task_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="开始时间"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 0"
            v-hasPermi="['task:basic:update']"
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="changeStatus(scope.row,1)"
          >启动</el-button>
          <el-button
            v-if="scope.row.status === 1"
            v-hasPermi="['task:basic:update']"
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="changeStatus(scope.row, 2)"
          >完成</el-button>
          <el-button
            v-if="scope.row.status === 1"
            v-hasPermi="['task:basic:update']"
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="changeStatus(scope.row, 3)"
          >终止</el-button>
          <el-button
            v-hasPermi="['task:basic:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            v-hasPermi="['task:basic:query']"
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button>
          <!-- <el-button
          v-hasPermi="['task:basic:query']"
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button>
          <el-button
          v-hasPermi="['task:basic:query']"
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="30%"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          label="作业列表："
          prop="jobId"
        >
          <el-select
            v-model="form.jobId"
            filterable
            placeholder="请选择生成任务的作业模板"
            style="width:100%"
          >
            <el-option
              v-for="dict in jobList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, delTask, updateTask, addTask } from '@/api/task/task'
import { listInfo } from '@/api/job/api'

export default {
  name: 'TaskList',
  dicts: ['sys_business_type', 'sys_task_status'],
  props: {
    corpName: {
      // 不为空说明是在客户详情页，没有搜索等功能
      type: String,
      default: ''
    },
    corpId: {
      // 不为空说明是在客户详情页，没有搜索等功能
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      form: {},
      loading: true,
      jobList: [],
      // 是否显示弹出层
      open: false,
      selected: [],
      title: '',
      // 显示搜索条件
      showSearch: !this.corpName,
      // 总条数
      total: 0,
      // 角色表格数据
      taskList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        corpName: this.corpName || undefined,
        name: undefined,
        type: undefined,
        status: undefined
      },
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listTask(this.queryParams).then((response) => {
        this.taskList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
      const query = {}
      listInfo(query).then((res) => {
        this.jobList.splice(0, this.jobList.length)
        for (const x of res.data.records) {
          this.jobList.push({ value: x.id, label: x.name })
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    reset() {
      this.form = {
        jobId: null
      }
      this.resetForm('form')
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.open = true
      this.title = '添加处理记录'
    },
    handleAdd() {
      var that = this
      this.reset()
      this.open = true
      this.title = '添加巡查任务'
    },
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm() {
      var that = this
      this.$refs['form'].validate((valid) => {
        if (valid) {
          debugger
          that.form.corpId = that.corpId
          addTask(that.form).then((response) => {
            that.$modal.msgSuccess('新增成功')
            that.open = false
            that.getList()
          })
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selected.map((v) => v.id)
      this.$modal
        .confirm('是否确认删除选中的数据项？')
        .then(function() {
          return delTask(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
          this.$refs.table.clearSelection()
          this.selected = []
        })
        .catch(() => {})
    },
    // 导出过滤后的企业数据
    handleExport() {
      this.download(
        'security/taskInfo/export',
        this.queryParams,
        `任务概要信息表_${new Date().getTime()}.xlsx`
      )
    },
    goDetailPage(row) {
      this.$router.push({
        name: 'TaskDetail',
        params: {
          taskId: row.id,
          type: row.type || 'environment'
        }
      })
    },
    changeStatus(row, status) {
      row.status = status
      const params = { status: status }
      updateTask(row, params).then((res) => {
        this.getList()
      })
    }
  }
}
</script>
